<!--
 * @Author: your name
 * @Date: 2020-12-07 14:15:40
 * @LastEditTime: 2021-05-14 15:45:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\warranty\warrantyManage.vue
-->
<template>
    <div class="warrantyManage-mian-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.SN')}}</span>
                <el-input v-model="inverterSn" v-big style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('site.siteName')}}</span>
                <el-input v-model="siteName" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('warrantyManage.userName')}}</span>
                <el-input v-model="loginName" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.deviceTest.wifiSN')}}</span>
                <el-input v-big v-model="wifiSN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('battery.batterySN')}}</span>
                <el-input v-big v-model="batterySN" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('warrantyManage.mailbox')}}</span>
                <el-input  v-model="email" style="width:160px;" :placeholder="$t('pleaseInput')"></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('warranty.Approval.Status')}}</span>
                <el-select v-model="auditState" filterable :placeholder="$t('index.selectStates')" clearable >
                    <el-option
                        v-for="item in approvalList"
                        :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                </el-select>
            </div>
            <!-- <div class="search-box-sub">
                <span class="lable-text">{{$t('warranty.sent.Status')}}</span>
                <el-select v-model="notifyState" filterable :placeholder="$t('index.selectStates')" clearable >
                    <el-option
                        v-for="item in sentrovalList"
                        :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                </el-select>
            </div> -->
            <div class="search-box-sub">
                <span class="lable-text">{{$t('m_497927264244072448')}}</span>
                <el-select v-model="enableFlag" filterable :placeholder="$t('index.selectStates')" clearable >
                    <el-option
                        v-for="item in dictionary.ON_LINE_STATE"
                        :key="item.value"
                        :label="item.code"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.nation')}}</span>
                <el-select v-model="nation" filterable :placeholder="$t('index.selectStates')" clearable >
                    <el-option
                        v-for="item in nationList"
                        :key="item.enName"
                        :label="item.enName"
                        :value="item.enName">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('device.inverter.inverterType')}}</span>
                <el-select v-model="model" filterable :placeholder="$t('index.selectStates')" clearable >
                    <el-option
                        v-for="item in inverterType"
                        :key="item.sid"
                        :label="item.inverterTypeName"
                        :value="item.inverterTypeName">
                    </el-option>
                </el-select>
            </div>
            <!-- <div class="search-box-sub">
                <span class="lable-text">{{$t('index.type')}}</span>
                <el-select v-model="type" filterable :placeholder="$t('index.selectStates')" clearable @change='changeTypeData'>
                    <el-option
                        v-for="item in typeListData"
                        :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                </el-select>
            </div> -->
            <div class="search-box-sub">
                <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-popconfirm
                    :confirm-button-text="$t('sure')"
                    :cancel-button-text="$t('cancel')"
            :title="$t('index.delete_confirm')+'?'"
            @confirm='qualityDeleteBatch()'
            >
                <el-button   type="primary" slot="reference"  icon='el-icon-delete' class="marginR10" >{{$t('delete')}}</el-button>
            </el-popconfirm>
            <el-popconfirm
                    :confirm-button-text="$t('sure')"
                    :cancel-button-text="$t('cancel')"
            :title="$t('warranty.batchSend.message')"
            @confirm='qualitySendBatch()'
            >
                <el-button   type="primary" slot="reference"  icon='el-icon-position' class="marginR10" >{{$t('warranty.batchSend')}}</el-button>
            </el-popconfirm>
            <el-button   type="primary" @click='showAuditModal'  icon='iconfont icon-yanjing' class="marginR10" >{{$t('warranty.batchExamine')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='true' @selection-change="handleSelectionChange($event)">
            <template #installTime='{data}'>
                {{data.installTime?data.installTime.split(' ')[0]:''}}
            </template>
            <template #type='{data}'>
                {{returnTypeText(data)}}
            </template>
            <template #expireTime='{data}'>
                {{data.expireTime?data.expireTime.split(' ')[0]:''}}
            </template>
            <template #option='{data}'>
                <div class="option-box">
                    <el-button size='small' type="primary" icon="el-icon-edit"  @click='showEditModal(data)' class="marginR10" :title="$t('edit')"></el-button>

                    <el-button v-if='(!data.auditState || data.type == 1 ) && data.type !=2' size='small' type="primary" icon="iconfont icon-yanjing"  @click="()=>{$store.commit('setVisibleAdd2',true);editRow = data}" class="marginR10" :title="$t('version.versionService.check')"></el-button>
                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                    :title="$t('version.versionAdd.RetrialStatus')"
                    @confirm='retrial(data)'
                    v-if='data.auditState == 1 && data.type != 1'
                    >
                        <el-button slot="reference"  size='small' type="primary" icon="iconfont icon-jurassic_loseeyes"  class="marginR10" :title="$t('version.versionAdd.Retrial')" style="color:red;" ></el-button>
                    </el-popconfirm>

                    <el-button v-if='data.auditState' size='small' type="primary" icon="el-icon-search"  @click='Zoom(data)' class="marginR10" :title="$t('warranty.zoom')"></el-button>

                    <el-button v-if='data.auditState && data.type != 1&& data.type != 2' size='small' type="primary" icon="el-icon-download"  @click='downloadPdf(data)' class="marginR10" :title="$t('devUpgrade.download')" ></el-button>
                    <el-popconfirm
                    :confirm-button-text="$t('sure')"
                    :cancel-button-text="$t('cancel')"
                    :title="$t('warrantyStatistics.send')"
                    @confirm='send(data)'
                    v-if='data.auditState&& data.type != 1&& data.type != 2'
                    >
                        <el-button  slot="reference" size='small' type="primary" icon="el-icon-s-promotion"   class="marginR10" :title="$t('user.updateUserName.send')" :style="data.notifyState == 1?'color:#50a1e6;':''"></el-button>

                    </el-popconfirm>
                    <el-popconfirm
                            :confirm-button-text="$t('sure')"
                            :cancel-button-text="$t('cancel')"
                        :title="$t('index.delete_item_confirm')"
                        @confirm='deleteRow(data.sid)'
                    >
                        <el-button class="marginR10" type="danger" slot="reference" icon='el-icon-delete' :title="$t('delete')"></el-button>
                    </el-popconfirm>
                    <el-button v-if='data.batterySN && !((data.auditState == 0 && data.type == 0) || data.type == 2 )' size='small' type="primary" icon="iconfont icon-dianchi"  @click="()=>{$store.commit('setApiVisible',true);editRow = data}" class="marginR10" :title="$t('batteryAnalysis.title')"></el-button>
                    <!-- <el-button size='small' type="primary" icon="iconfont icon-remove-site"  @click='showRejectModal(data)' class="marginR10" :title="$t('warranty.reject')"></el-button> -->
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <edit :nationList='nationList' :inverterType='inverterType' @uploadData='qualityGetPage' :editRow='editRow'></edit>
        <detailModal :editRow='editRow'></detailModal>
        <examine :editRow='editRow' :inverterType='inverterType' @uploadData='qualityGetPage'></examine>
        <reject @uploadData='qualityGetPage'></reject>
        <batchAudit :selectDataList='selectDataList' @uploadData='qualityGetPage'></batchAudit>
    </div>
</template>

<script>
import baseTable from '../../../components/common/baseTable'
// import add from './add'
import edit from './edit'
import reject from './reject'
import detailModal from './batteryDetail.vue'
import examine from './examine'
import batchAudit from './batchAudit'
export default {
    components: {
        baseTable,
        // add,
        edit,
        detailModal,
        examine,
        reject,
        batchAudit
    },
    props: {

    },
    data() {
        return {
            inverterSn:'',
            siteName:'',
            loginName:'',
            wifiSN:'',
            enableFlag:'',
            nation:'',
            model:'',
            email:'',
            batterySN:'',
            size:10,
            current:1,
            total:1,
            dictionary:[],
            nationList:[],
            inverterType:[],
            approvalList:[{
                id:'0',
                text:this.$t('warranty.add.msg')
            },{
                id:'4',
                text:this.$t('warranty.Retrial.msg')
            },{
                id:'1',
                text:this.$t('warranty.Approval.Reviewed')
            },{
                id:'2',
                text:this.$t('warranty.Approval.Sent')
            },{
                id:'3',
                text:this.$t('warranty.reject.msg')
            }],
            // sentrovalList:[{
            //     id:'0',
            //     text:this.$t('warranty.Approval.notSent')
            // },{
            //     id:'1',
            //     text:this.$t('warranty.Approval.Sent')
            // }],
            dataRows:[],
            column:[{
                title: this.$t('devWifi.deviceSN'),
                value: 'inverterSn',
                slot: 'inverterSn',
            },
            {
                title: this.$t('devWifi.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
                width:'130'
            },
            {
                title: this.$t('device.inverter.inverterType'),
                value: 'model',
                slot: 'model',
            },
            // {
            //     title: this.$t('totalPower.InverterPower.siteId'),
            //     value: 'siteName',
            //     slot: 'siteName',
            // },{
            //     title: this.$t('warrantyManage.userName'),
            //     value: 'loginName',
            //     slot: 'loginName',
            // },
            {
                title: this.$t('warrantyStatistics.nation'),
                value: 'nation',
                slot: 'nation',
                width:'130'
            }
            ,{
                title: this.$t('battery.accessTime'),
                value: 'accessTime',
                slot: 'accessTime',
                width:'130'
            }
            ,{
                title: this.$t('salePage.tradeTime'),
                value: 'installTime',
                slot: 'installTime',
                width:'130'
            },
            {
                title: this.$t('warrantyManage.warranty_period'),
                value: 'expireTime',
                slot: 'expireTime',
                width:'130',
            },
            {
                title: this.$t('index.type'),
                value: 'type',
                slot: 'type',
                width:'100',
            },
            {
                title: this.$t('control'),
                value: 'option',
                slot: 'option',
            }],
            loading:false,
            editRow:{},
            selectDataList:[],
            auditState:"",
            notifyState:'',
            
            type:''

        };
    },
    computed: {
        deviceType(){
            let type= ''
            this.inverterType.forEach(item =>{
                if(item.inverterTypeName == this.model){
                    type = item.inverterType
                }
            })
            return type
        }
    },
    watch: {
        total(){
            if(this.total==(this.current-1)*this.size&& this.total!=0){
                this.current-=1;
                if(this.current < 1){
                    this.current = 1
                }
                this.qualityGetPage();
            }
        }
    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.qualityGetPage()
        this.selectCountry()
        this.orderpaypalconfigGetInverterTypeList()
    },
    methods: {
        changeAuditState(){
            this.type=''
        },
        changeTypeData(){
            if(this.type == 1){
                this.auditState='0'
            }else if(this.type == 2){
                this.auditState = ''
            }else if(this.type == 3){
                this.auditState='0'
            }else if(this.type == 4){
                this.auditState='1'
            }
        },
        returnTypeText(data){
            if(data.auditState == 0){
                if(data.type == 0){
                    return this.$t('warranty.add.msg')
                }
                if(data.type == 1){
                    return this.$t('warranty.Retrial.msg')
                }
                if(data.type == 2){
                    return this.$t('warranty.reject.msg')
                }
            }else if(data.auditState == 1){
                if(data.type == 0){
                    return this.$t('warranty.Approval.Reviewed')
                }
                if(data.type == 1){
                    return this.$t('warranty.Retrial.msg')
                }
                if(data.type == 2){
                    return this.$t('warranty.reject.msg')
                }
            }else{
                return ' - '
            }
        },
        showAuditModal(){
            if(this.selectDataList.length>0){
                this.$store.commit('setRightVisibleEdit',true)
            }else{
                this.comm.showMsg(false,this.$t('device-ple-check'))
            }
            
        },
        showRejectModal(){
            this.$store.commit('setRightVisible',true)
        },
        async qualityCheckQualityBatch(){
            let rows=this.selectDataList
            if(rows.length == 0){
                this.comm.showMsg(false, this.$t('pleaseSelect'))
                return
            }
            let res = await this.API.qualityCheckQualityBatch({
                ids:rows.map(item =>{
                    return item.sid
                })
            })
            if(this.comm.showMsg(res.success)){
                this.qualityGetPage()
            }
        },
        async qualitySendBatch(){
            let rows=this.selectDataList
            if(rows.length == 0){
                this.comm.showMsg(false, this.$t('pleaseSelect'))
                return
            }
            for(var i=0;i<rows.length;i++){
                if(!rows[i].auditState){
                    this.comm.showMsg(false,this.$t('warranty.batchSend.message2'))
                    return
                }
            }
            let arr = []
            this.selectDataList.forEach(item=>{
                    arr.push(item.sid)
                })
            let res = await this.API.qualitySendInvoiceToUserBatch({
                ids:arr
            })
            if(this.comm.showMsg(res.object.waitSendList.length>0,this.$t('warranty.batchSend.message3'))){
                    this.qualityGetPage()
                    return 
            }
            if(this.comm.showMsg(res.success)){
                this.qualityGetPage()
            }
        },
        async send(row){
            let data = await this.API.qualityGetById({
                id:row.sid
            })
            let res2 = await this.API.qualitySendInvoiceToUser({
                id:data.sid,inverterSn:data.inverterSn,email:data.email,inverterType:data.deviceType
            })
            if(this.comm.showMsg(res2.success)){
                this.qualityGetPage()
            }
        },
        Zoom(row){
            let editRow = row;
	    	var deviceTypeName=editRow.deviceType == 16?editRow.deviceTypeName:this.comm.returnCode(this.inverterType,editRow.deviceType,'inverterType','inverterTypeName')
            // window.open(process.env.VUE_APP_BASE_PROXY + process.env.VUE_APP_BASE_API2+'/quality/previewQualityInvoice.do?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
            this.comm.windowOpen('/quality/previewQualityInvoice?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
        },
        downloadPdf(row){
            let editRow = row;
	    	var deviceTypeName=editRow.deviceType == 16?editRow.deviceTypeName:this.comm.returnCode(this.inverterType,editRow.deviceType,'inverterType','inverterTypeName')
            // window.open('quality/downloadQualityInvoice.do?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
            this.comm.windowOpen('/quality/downloadQualityInvoice?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
        },
        async retrial(row){
            let res = await this.API.qualityCancleCheckQuality({
                id:row.sid,
                inverterSn:row.inverterSn
            })
            if(this.comm.showMsg(res.success)){
                this.qualityGetPage()
            }
        },
        async deleteRow(id){
            let res = await this.API.qualityDelete({
                id
            })
            if(this.comm.showMsg(res.success)){
                this.qualityGetPage()
            }
        },
        async qualityDeleteBatch(){
            if(this.selectDataList.length == 0){
                this.comm.showMsg(false,this.$t('index.delete_select'))
                return
            }
            let arr = []
            this.selectDataList.forEach(item=>{
                    arr.push(item.sid)
                })
            let res = await this.API.qualityDeleteBatch({
                ids:arr
            })
            if(this.comm.showMsg(res.success)){
                this.qualityGetPage()
            }
        },
        pushRenew(data){
            sessionStorage.setItem("selRow",JSON.stringify(data));
            this.$router.push({path:"extendedManagement_renew"})
        },
        downloadModal(row){
            let editRow=row;
            let deviceTypeName=editRow.deviceType == 16?editRow.deviceTypeName:this.comm.returnCode(this.inverterType,editRow.deviceType,'inverterType','inverterTypeName')
	    	window.open(process.env.VUE_APP_BASE_PROXY + process.env.VUE_APP_BASE_API2+'/quality/downloadQualityInvoice.do?id='+editRow.sid+'&inverterSn='+editRow.inverterSn+'&inverterType='+deviceTypeName)
        },
        showEditModal(data){
            this.editRow = data
            this.$store.commit('setVisibleEdit',true)
        },
        
        async qualityGetPage(){
            this.loading = true
            let type = ''
            let auditState = this.auditState
            // if(this.type == 1){
            //     type = 0
            // }else if(this.type == 2){
            //     type = 1
            // }else if(this.type == 3){
            //     type = 2
            // }else if(this.type == 4){
            //     type = 0
            //     auditState=1
            // }
            if(this.auditState === '0'){
                auditState = 0
                type = 0
            }else if(this.auditState == 1){
                type = 0
                auditState = 1
            }else if(this.auditState == 2){
                auditState = 5 
                type = ''
            }else if(this.auditState == 3){
                auditState = ''
                type = 2
            }else if(this.auditState == 4){
                type = 1
                auditState = ''
            }

            let res = await this.API.qualityGetPage({
                size: this.size,
                current: this.current,
                inverterSn: this.inverterSn,
                siteName: this.siteName,
                loginName: this.loginName,
                wifiSN: this.wifiSN,
                enableFlag: this.enableFlag,
                nation: this.nation,
                model:this.model,
                deviceType:this.deviceType,
                auditState:auditState == 5?'':auditState,
                notifyState:auditState == 5?1:'',
                email:this.email,
                batterySN:this.batterySN,
                type:type
            })
            this.loading = false
            this.dataRows = res.rows || []
            this.total = res.total
        },
        async selectCountry(){
            let res = await this.API.selectCountry()
            this.nationList = res
        },
        async orderpaypalconfigGetInverterTypeList(){
            let res = await this.API.orderpaypalconfigGetInverterTypeList({
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.inverterType = res
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:'ON_LINE_STATE,WARRANT_INVERTER_TYPES,IS_PUBLIC,SALER_TYPE,INTELBARS_INVERTER_TYPES',
                lang:localStorage.getItem('lang')||'zh_CN'
            })
            this.dictionary = res || {}
        },
        query(){
            this.current = 1
            this.qualityGetPage()
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.qualityGetPage()
        },
        handleCurrentChange(val) { 
          this.current = val
          this.qualityGetPage()
        },
        handleSelectionChange(val){
          this.selectDataList=val
        },
    },
};
</script>

<style  lang="less" scoped>
.warrantyManage-mian-box{
    
}
</style>

